package com.lottery.ruok.lottrey.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.MyViewPagerAdapter;
import com.lottery.ruok.lottrey.adapter.PlayRuleAdapter;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetRuleRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetRuleResponse;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import utils.Utils;

/**
 * Created by ASUS on 2017/8/8.
 * 彩票规则
 *
 */

public class PlayRuleActivity extends BaseActivity {
    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.vp_view)
    ViewPager vpView;
    private ArrayList<GetRuleResponse> getRuleResponses;

    private ArrayList<View> mViewList = new ArrayList<>();//页卡视图集合

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_rule);
        ButterKnife.bind(this);

        init();
    }

    private View getTabView(String s, int position, int index){
        View view = LayoutInflater.from(this).inflate(R.layout.tab_item, null);
        TextView tv= view.findViewById(R.id.textView);
        tv.setText(s);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        if(position == index){
            view.findViewById(R.id.imageView).setVisibility(View.VISIBLE);
        }
        return view;
    }

    private void init() {
        RetrofitBase.getRetrofit().create(GetRuleRequest.class).getResult()
                .compose(RxHelper.<ArrayList<GetRuleResponse>>handleResult())
                .subscribe(new RxSubscribe<ArrayList<GetRuleResponse>>(this) {
                    @Override
                    protected void _onNext(ArrayList<GetRuleResponse> g) {
                        getRuleResponses = g;
                        String[] string_array = new String[getRuleResponses.size()];

                        for (int i = 0; i < getRuleResponses.size(); i++) {
                            tabs.addTab(tabs.newTab().setText(getRuleResponses.get(i).getName()));//添加tab选项卡
                            View view = LayoutInflater.from(PlayRuleActivity.this).inflate(R.layout.play_rule_item, null);
                            ListView listView = (ListView) view;
                            PlayRuleAdapter myAdapter = new PlayRuleAdapter(i, PlayRuleActivity.this, getRuleResponses);
                            TextView textView = new TextView(PlayRuleActivity.this);
                            AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                            textView.setText(getRuleResponses.get(i).getItem_desc());
                            textView.setTextColor(Color.parseColor("#272727"));
                            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
                            textView.setLineSpacing(Utils.dip2px(PlayRuleActivity.this, 5), 1.0f);
                            textView.setBackgroundResource(R.color.bg_white);
                            textView.setPadding(Utils.dip2px(PlayRuleActivity.this, 18), Utils.dip2px(PlayRuleActivity.this, 9), Utils.dip2px(PlayRuleActivity.this, 18), Utils.dip2px(PlayRuleActivity.this, 9));
                            textView.setLayoutParams(layoutParams);
                            listView.addHeaderView(textView);
                            listView.setAdapter(myAdapter);

                            string_array[i] = getRuleResponses.get(i).getName();

                            mViewList.add(view);
                        }

                        MyViewPagerAdapter mAdapter = new MyViewPagerAdapter(mViewList, string_array);
                        vpView.setAdapter(mAdapter);//给ViewPager设置适配器
                        tabs.setupWithViewPager(vpView);//将TabLayout和ViewPager关联起来。
                        tabs.setTabsFromPagerAdapter(mAdapter);//给Tabs设置适配器

                        int index = 0;
                        if (getIntent().getStringExtra("code") != null) {
                            for (int i = 0; i < mViewList.size(); i++) {
                                if (getIntent().getStringExtra("code").equals(getRuleResponses.get(i).getCode())) {
                                    index = i;
                                    break;
                                }
                            }
                        }
                        for (int i = 0; i < tabs.getTabCount(); i++) {
                            TabLayout.Tab tab = tabs.getTabAt(i);
                            if (tab != null) {
                                tab.setCustomView(getTabView(getRuleResponses.get(i).getName(), i, index));
                            }
                        }
                        vpView.setCurrentItem(index);

                        vpView.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                            @Override
                            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                            }

                            @Override
                            public void onPageSelected(int position) {
                                for (int i = 0; i < tabs.getTabCount(); i++) {
                                    if (i == position) {
                                        tabs.getTabAt(i).getCustomView().findViewById(R.id.imageView).setVisibility(View.VISIBLE);
                                    } else {
                                        tabs.getTabAt(i).getCustomView().findViewById(R.id.imageView).setVisibility(View.GONE);
                                    }
                                }
                            }

                            @Override
                            public void onPageScrollStateChanged(int state) {

                            }
                        });
                    }

                    @Override
                    protected boolean showDialog() {
                        return true;
                    }
                });
    }
}
